import 'package:car_map/mode/temperature_model.dart';
import 'package:car_map/page/res/gaps.dart';
import 'package:car_map/page/res/styles.dart';
import 'package:car_map/widget/load_image.dart';
import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';

///温度列表详情
class WidgetTemperatureExpansion extends StatefulWidget {
  @override
  _widgetTemperatureExpansionState createState() =>
      _widgetTemperatureExpansionState();
}

class _widgetTemperatureExpansionState
    extends State<WidgetTemperatureExpansion> {
  List<TemperatureModel> temperatureModelList;

  @override
  void initState() {
    // TODO: implement initState
    super.initState();
    getTemperatureModelList();
  }

  @override
  Widget build(BuildContext context) {
    // TODO: implement build
    return ExpansionpanelWidget();
  }

  Widget ExpansionpanelWidget(){

    return Container(
      width: double.infinity/2,
      child:  ExpansionPanelList(
        expansionCallback: (expan, isExpanded) {
          setState(() {
            temperatureModelList[expan].isChange = !isExpanded;
          });
        },
        children: temperatureModelList.map((item) {
          return new ExpansionPanel(
            headerBuilder: (context, isExpanded) {
              return getTemperatureItem(item);
            },
            body: Container(
              height: ScreenUtil().setHeight(item.data.length * 250.0),
              child: ListView.builder(
                itemBuilder: (context, index) {
                  return getContentTemperatureItem(item.data[index],item.license);
                },
                itemCount: item.data.length,
              ),
            ),
            isExpanded: item.isChange,
          );
        }).toList(),
      ),
    );
  }

  //加油详情item
  Widget getContentTemperatureItem(
      TemperatureExpansionpanleData expansionpanleData, String license) {
    return Container(
      alignment: Alignment.center,
      height: ScreenUtil().setHeight(250.0),
      padding: EdgeInsets.only(left: 30.0, right: 10.0, top: 5.0, bottom: 5.0),
      decoration: BoxDecoration(
          color: Colors.black12,
          border: Border.all(color: Colors.black12, width: 0.5)),
      child: Column(
        mainAxisAlignment: MainAxisAlignment.center,
        crossAxisAlignment: CrossAxisAlignment.start,
        children: <Widget>[
          Text(
            '车牌号码: ${license}',
            style: TextStyles.textDarkGray14,
          ),
          Gaps.vGap5,
          Row(
            mainAxisAlignment: MainAxisAlignment.spaceBetween,
            children: <Widget>[
              Text(
                '时间:${expansionpanleData.timeDate}',
                style: TextStyles.textDarkGray14,
              ),
              Gaps.vGap5,
              Text(
                '速度:${expansionpanleData.speed}',
                style: TextStyles.textDarkGray14,
              ),
            ],
          ),
          Gaps.vGap5,
          Row(
            mainAxisAlignment: MainAxisAlignment.spaceBetween,
            children: <Widget>[
              Text(
                '温度:${expansionpanleData.temperature}',
                style: TextStyles.textDarkGray14,
              ),
              Gaps.vGap5,
              Text(
                '平均温度:${expansionpanleData.avgTemperature}',
                style: TextStyles.textDarkGray14,
              ),
            ],
          ),
          Gaps.vGap5,
          Text(
            '湿度:${expansionpanleData.humidity}',
            style: TextStyles.textDarkGray14,
          ),
          Gaps.vGap5,
          Text(
            '地址:${expansionpanleData.address}',
            style: TextStyles.textDarkGray14,
          )
        ],
      ),
    );
  }

  //加油item
  Widget getTemperatureItem(TemperatureModel temperatureModel) {
    return Container(
      child: ListTile(
        title: Text(
          '${temperatureModel.license}',
          style: TextStyles.textSize16,
        ),
        leading: LoadAssetImage(
          "statistics/ic_car_no",
          width: ScreenUtil().setWidth(50.0),
          height: ScreenUtil().setHeight(50.0),
        ),
      ),
    );
  }

  //数据处理
  void getTemperatureModelList() {
    temperatureModelList = new List();

    List<TemperatureExpansionpanleData> temperatureExpansionpanleList =
        new List();
    temperatureExpansionpanleList
      ..add(new TemperatureExpansionpanleData(
          timeDate: '2019-11-01 11:37:20',
          speed: 60.0,
          temperature: 45.0,
          avgTemperature: 50.0,
          humidity: 0.4,
          status: '搅拌',
          address: '陕西省西安市雁塔区科技四路'))
      ..add(new TemperatureExpansionpanleData(
          timeDate: '2019-11-01 11:37:20',
          speed: 60.0,
          temperature: 45.0,
          avgTemperature: 50.0,
          humidity: 0.4,
          status: '搅拌',
          address: '陕西省西安市莲湖区龙首村'))
      ..add(new TemperatureExpansionpanleData(
          timeDate: '2019-11-01 11:37:20',
          speed: 60.0,
          temperature: 45.0,
          avgTemperature: 50.0,
          humidity: 0.4,
          status: '搅拌',
          address: '陕西省西安市新城区'));

    TemperatureModel temperatureModel = new TemperatureModel(
        data: temperatureExpansionpanleList,
        license: '陕C00001',
        isChange: false);

    TemperatureModel temperatureModel2 = new TemperatureModel(
        data: temperatureExpansionpanleList,
        license: '陕C00002',
        isChange: false);


    TemperatureModel temperatureModel3 = new TemperatureModel(
        data: temperatureExpansionpanleList,
        license: '陕C00003',
        isChange: false);


    temperatureModelList.add(temperatureModel);
    temperatureModelList.add(temperatureModel2);
    temperatureModelList.add(temperatureModel3);
  }
}
